﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KTXD.Entities;
using KTXD.Data;

namespace UserKTXD.DCTEDU_.DanhMuc
{
    public partial class DMBoSuuTap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Bind_Data();
            }
        }

        protected void Bind_Data()
        {
            TList<BoSuuTap> TList_Catalog = DataRepository.BoSuuTapProvider.GetAll();
            gridCatalog.DataSource = TList_Catalog;
            gridCatalog.DataBind();
        }

        protected void cmdSave_Click(object sender, ImageClickEventArgs e)
        {
            bool updateStatus = false;
            try
            {
                BoSuuTap entBoSuuTap = new BoSuuTap();
                entBoSuuTap.Id = Convert.ToInt32(H_CatalogId.Value); ;
                entBoSuuTap.Ten = Common.StandardString(txtTen.Text);
                entBoSuuTap.DienGiai = Common.StandardString(txtDienGiai.Text);
                entBoSuuTap.TrangThai = chkSuDung.Checked;
                entBoSuuTap.NgayCapNhat = Common.GetServerDateTime();

                if (entBoSuuTap.Id == 0)
                {
                    entBoSuuTap.Id = 0;
                    updateStatus = DataRepository.BoSuuTapProvider.Insert(entBoSuuTap);
                }
                else
                {
                    entBoSuuTap.EntityState = EntityState.Changed;
                    updateStatus = DataRepository.BoSuuTapProvider.Update(entBoSuuTap);
                }
                if (updateStatus)
                {
                    Bind_Data();
                    cmdReset_Click(sender, e);
                }
                else
                    WebMessage.Show("Cập nhật Bộ sưu tập thành công!");
            }
            catch (Exception)
            {
                WebMessage.Show("Cập nhật Bộ sưu tập thất bại!");
            }
        }

        protected void cmdReset_Click(object sender, ImageClickEventArgs e)
        {
            txtTen.Text = "";
            txtDienGiai.Text = "";
            chkSuDung.Checked = false;
            H_CatalogId.Value = "0";
        }

        protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";
            }
        }

        protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                try
                {
                    H_CatalogId.Value = e.CommandArgument.ToString();
                    BoSuuTap entBoSuuTap = DataRepository.BoSuuTapProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                    if (entBoSuuTap != null)
                    {
                        txtTen.Text = entBoSuuTap.Ten;
                        txtDienGiai.Text = entBoSuuTap.DienGiai;
                        chkSuDung.Checked = entBoSuuTap.TrangThai;
                    }
                }
                catch (Exception)
                {
                }
            }
            if (e.CommandName == "Delete")
            {
                try
                {
                    TList<HinhAnh> Tlist_HinhAnh = DataRepository.HinhAnhProvider.GetByIdBoSuuTap(Convert.ToInt32(e.CommandArgument.ToString()));
                    if (Tlist_HinhAnh.Count > 0)
                    {
                        string sFilePath = Server.MapPath(Common.sPathUpBoSuuTap);
                        foreach (HinhAnh entHinhAnh in Tlist_HinhAnh)
                        {
                            string sThumbPath = sFilePath + "\\" + entHinhAnh.Id.ToString() + "\\Thumb\\" + entHinhAnh.Ten;
                            string sFullPath = sFilePath + "\\" + entHinhAnh.Id.ToString() + "\\Full\\" + entHinhAnh.Ten;
                            if (System.IO.File.Exists(sThumbPath))
                            {
                                System.IO.File.Delete(sThumbPath);
                            }
                            if (System.IO.File.Exists(sFullPath))
                            {
                                System.IO.File.Delete(sFullPath);
                            }
                            DataRepository.HinhAnhProvider.Delete(entHinhAnh.Id);
                        }
                    }
                    bool deleteStatus = DataRepository.BoSuuTapProvider.Delete(Convert.ToInt32(e.CommandArgument.ToString()));
                    if (!deleteStatus)
                    {
                        WebMessage.Show("Xóa Bộ sưu tập thất bại");
                    }
                }
                catch (Exception)
                {
                    WebMessage.Show("Xóa Bộ sưu tập thất bại");
                }
            }
        }

        protected void gridCatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            txtTen.Text = "";
            txtDienGiai.Text = "";
            chkSuDung.Checked = false;
            H_CatalogId.Value = "0";
            Bind_Data();
        }
    }
}